/**
 * Created by CK on 2016/11/8.
 */
$(document).ready(function(){
    var appCode   = xhTool.getQuery('iAppCode');
    var schoolId  = xhTool.getQuery('iSchoolId');
    var list      = $('#List');
    var startDom  = $('#List_Search_StartTime');
    var endDom    = $('#List_Search_EndTime');
    var startTime = xhTool.getQuery('iStartTime');
    var endTime   = xhTool.getQuery('iEndTime');
    var subject   = xhTool.getQuery('sSubject', true);
    var stageDom  = $('#List_Search_Stage');
    var stageFlag;
    if(!startTime){
        startDom.datebox('setValue', GetDateStr(-1));
        startTime = startDom.getElementValue();
    }
    else{
        startDom.datebox('setValue', startTime);
    }
    if(!endTime){
        endDom.datebox('setValue', GetDateStr(-1));
        endTime   = endDom.getElementValue();
    }
    else{
        endDom.datebox('setValue', endTime);
    }
    var queryParams = {
        iAppCode: appCode,
        iSchoolId: schoolId,
        iStartTime: startTime,
        iEndTime: endTime
    };
    if(subject) {
        queryParams.sSubject = subject;
    }
    list.initDataGrid(baseUrl + 'Stats/getSingleSchoolData',{queryParams: queryParams});
    //判断是否有coId，有则更新下拉框选项，否则隐藏下拉框
    if(list.datagrid('getColumnOption', 'iCoId')){
        stageDom.combobox({
            panelHeight: 'auto',
            editable: false,
            textField: 'sStageName',
            valueField: 'sCoIds',
            url: baseUrl + 'Stats/getSchoolStageList?iSchoolId=' + schoolId
        });
        stageDom.next('.combo').css('margin-left', '10px');
        stageDom.next('.combo').find('input').attr('placeholder', '选择期次');
        stageFlag = true;
    }
    else{
        //stageDom.combobox('destroy');
        stageDom.hide();
        stageFlag = false;
    }
    $('#bt_list_search').click(function(){
        getParams();
        list.addDataGridParams(queryParams);
    });
    DataGrid.onDblClickCell = function(rowIndex, field, value){
        var data = list.datagrid('getRows');
        data = data[rowIndex];
        var params = '?iAppCode=' + appCode + '&iSchoolId=' + schoolId
            + '&iStartTime=' + startTime + '&iEndTime=' + endTime + '&sClassId=' + data.sClassId;
        if(subject){
            params = params + '&sSubject=' + subject;
        }
        if(data.iCoId){
            params = params + '&iCoId=' + data.iCoId;
        }
        if(data.iTchId){
            params = params + '&iTchId=' + data.iTchId;
        }
        if(field == 'sClassName' || field == 'iStuNum'){
            window.top.xhTop.createNewTab('统计_班级学生_' + data.sClassName.replace(/<\/?.+?>/g,"").replace(/&nbsp;/g,""),
                baseUrl + 'Stats/classStuInfo' + params
            );
        }
        if(field == 'iTaskFixNum'){
            window.top.xhTop.createNewTab('统计_班级作业_' + data.sClassName.replace(/<\/?.+?>/g,"").replace(/&nbsp;/g,""),
                baseUrl + 'Stats/classTaskInfo' + params
            );
        }
        return false;
    };
    $('#bt_list_Export').click(function(){
        getParams();
        location.href = baseUrl + 'Stats/exportSingleSchoolData?' + parseParam(queryParams);
        // window.open(baseUrl + 'Stats/exportSingleSchoolData?' + parseParam(queryParams));
        list.addDataGridParams(queryParams);
    });
    function getParams(){
        startTime = startDom.getElementValue();
        endTime   = endDom.getElementValue();
        if(!startTime || !endTime){
            alert('时间不可为空！');
            return false;
        }
        if(stageFlag){
            var coIds = stageDom.getElementValue(true).join(',');
            if(coIds){
                queryParams.coIds   = coIds;
            }
            else{
                delete queryParams.coIds
            }
        }
        queryParams.iStartTime = startTime;
        queryParams.iEndTime   = endTime;
    }
});
function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth()+1;//获取当前月份的日期
    var d = dd.getDate();
    return y+"-"+m+"-"+d;
}
var parseParam = function(param, key){
    var paramStr="";
    if(param instanceof String||param instanceof Number||param instanceof Boolean){
        paramStr+="&"+key+"="+encodeURIComponent(param);
    }else{
        $.each(param,function(i){
            var k=key==null?i:key+(param instanceof Array?"["+i+"]":"."+i);
            paramStr+='&'+parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};